package edu.gdkm.sql.dao;

import edu.gdkm.sql.pojo.Userinfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserinfoMapper {

    // 查询全部用户信息
    @Select("SELECT * FROM userinfo")
    @Results(id = "userinfoMap",value = {
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "name", column = "name"),
            @Result(property = "address", column = "address"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "order", column = "id",
                    one = @One(select = "edu.gdkm.sql.dao.OrderMapper.findById"))
    })
    List<Userinfo> findAll();

    // 根据id查询用户信息
    @Select("SELECT * FROM userinfo WHERE id = #{id}")
    @ResultMap("userinfoMap")
    Userinfo findById(int id);

    // 根据用户名查询用户信息
    @Select("SELECT * FROM userinfo WHERE username = #{username}")
    @ResultMap("userinfoMap")
    Userinfo findByUsername(String username);

    // 插入用户信息
    @Insert("INSERT INTO userinfo(username, password, name, address, phone) VALUES(#{username}, #{password}, #{name}, #{address}, #{phone})")

    @ResultMap("userinfoMap")
    int insertUserinfo(Userinfo userinfo);

    // 更新用户信息
    @Update("UPDATE userinfo SET username=#{username}, password=#{password}, name=#{name}, address=#{address}, phone=#{phone} WHERE id=#{id}")
    @ResultMap("userinfoMap")
    int updateUserinfoById(Userinfo userinfo);

    // 删除用户信息
    @Delete("DELETE FROM userinfo WHERE id=#{id}")
    @ResultMap("userinfoMap")
    int deleteUserinfoById(int id);
}